﻿namespace CachePage
{
    using Extensions.Web.UI;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.Adapters;

    public class ExtenderControlAdapter : ControlAdapter
    {
        private void ExtenderOverlayScriptManager(ExtenderControl targetControl)
        {
            ScriptManager manager = new ScriptManager();
            typeof(ExtenderControl).GetField("_scriptManager", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(targetControl, manager);
            object obj2 = typeof(ScriptManager).GetProperty("ScriptControlManager", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(manager, null);
            object obj3 = obj2.GetType().GetProperty("ExtenderControls", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj2, null);
            List<Control> list = new List<Control>();
            Control item = targetControl.FindControl(targetControl.TargetControlID);
            list.Add(item);
            object obj4 = obj2.GetType().GetField("_extenderControls", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj2);
            FieldInfo field = obj4.GetType().GetField("_dictionary", BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo info6 = obj4.GetType().GetField("_keys", BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo info7 = obj4.GetType().GetField("_values", BindingFlags.NonPublic | BindingFlags.Instance);
            Dictionary<IExtenderControl, List<Control>> dictionary = (Dictionary<IExtenderControl, List<Control>>) field.GetValue(obj4);
            List<IExtenderControl> list2 = (List<IExtenderControl>) info6.GetValue(obj4);
            List<List<Control>> list3 = (List<List<Control>>) info7.GetValue(obj4);
            dictionary.Add(targetControl, list);
            list2.Add(targetControl);
            list3.Add(list);
        }

        private bool GetUserControlSuppressRegister(out UserControl SuppressControl)
        {
            for (Control control = base.Control.Parent; control.Parent != null; control = control.Parent)
            {
                if ((control is UserControl) && ((UserControl) control).SuppressExtenderControlScript)
                {
                    SuppressControl = (UserControl) control;
                    return true;
                }
            }
            SuppressControl = null;
            return false;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            bool userControlSuppressRegister = false;
            UserControl suppressControl = null;
            if (base.Page.PageAdapter is PageStateAdapter)
            {
                PageStateAdapter pageAdapter = (PageStateAdapter) base.Page.PageAdapter;
                if (pageAdapter.IsExtensionsPage)
                {
                    userControlSuppressRegister = this.GetUserControlSuppressRegister(out suppressControl);
                }
            }
            if (!userControlSuppressRegister)
            {
                base.Render(writer);
            }
            else
            {
                ExtenderControl control = (ExtenderControl) base.Control;
                if (suppressControl.OnSuppressExtenderControlScriptDescriptors(control))
                {
                    this.ExtenderOverlayScriptManager(control);
                    base.Render(writer);
                }
                else
                {
                    base.Render(writer);
                }
            }
        }
    }
}

